<!DOCTYPE html>
<html>

<head>
    <title>Preview Display</title>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="custom/images/favicon.ico?v=2" />
    <style>
        html,
        body {
            padding: 0px;
            margin: 0px;
        }
    </style>
    <script src='../libs/jquery-3.4.1.min.js'></script>
    <script src='../configs/htconfig.js'></script>
    <script src='../libs/echarts.js'></script>
    <script src='../libs/core/IZrowIfUVaXF.js'></script>
    <script src='../libs/core/VT3MIEkV8AKv.js'></script>
    <script src='../libs/core/ht.js'></script>
    <script src='../libs/core/buckle.js'></script>
    <script src='../libs/plugin/ht-edgetype.js'></script>
    <script src='../libs/plugin/ht-obj.js'></script>
    <script src='../libs/core/ht-ui.js'></script>
    <script src='../libs/plugin/ht-form.js'></script>
    <script src='../libs/plugin/ht-vector.js'></script>

    <script src='ajax/ajax.js'></script>
    <script src='ajax/jquery.table2excel.js'></script>
    <script>
        var dataModel = new ht.DataModel();
        var graphView = new ht.graph.GraphView(dataModel);

        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return (undefined);
        }


        function init() {
            ht.Default.xhrLoad('displays/jibei/偏航偏差机组.json', function (text) {
                graphView.addToDOM();
                var json = ht.Default.parse(text);
                if (json.title) document.title = json.title;

                dataModel.deserialize(json);


                getData(dataModel);//页面取值赋值

                graphView.fitContent(true)
            });
        }


        function getData(dataModel) {
            var area = dataModel.getDataByTag("area");//区域
            var station = dataModel.getDataByTag("station");//场站
            var fan = dataModel.getDataByTag("set");//机组
            var startTime = dataModel.getDataByTag("startTime");//开始时间
            var endTime = dataModel.getDataByTag("endTime");//结束时间
            var search = dataModel.getDataByTag("search");//搜索
            var heatchart = dataModel.getDataByTag("heatchart");//偏差角度热力图
            var positionDeviation1 = dataModel.getDataByTag("positionDeviation1");//偏航位置偏差仪表盘1
            var positionDeviation2 = dataModel.getDataByTag("positionDeviation2");//偏航位置偏差仪表盘2
            var table = dataModel.getDataByTag("table");//表格数据
            //getArea();
            var areaData = getArea();//获取区域名称以及id
            if (areaData != "") {
                area.a("ht.dataSource", areaData)
            }
            var jizu = getQueryVariable("unitId") == undefined ? 1162 : getQueryVariable("unitId");//从地址栏获取机组信息
            var areaValue = getQueryVariable("areaId") == undefined ? "" : getQueryVariable("areaId");
            var stationValue = getQueryVariable("stationId") == undefined ? 610 : getQueryVariable("stationId");
            var start = getQueryVariable("start");
            var end = getQueryVariable("end");
            var stationArr = getStation(areaValue);
            station.a("ht.dataSource", stationArr)
            var fanArr = getFan(stationValue);
            fan.a("ht.dataSource", fanArr)

            //if (jizu != "" && jizu != undefined) {


            // 设置下拉框默认值
            if (areaValue != "") {
                area.a("ht.value", parseInt(areaValue));
            } else {
                area.a("ht.value", "");
            }

            station.a("ht.value", parseInt(stationValue));
            fan.a("ht.value", parseInt(jizu));//地址栏传参附默认值

            startTime.a("ht.value", parseTime(start));
            endTime.a("ht.value", parseTime(end));

            // 初始化图标值
            angleThermodynamicChart(jizu, start, end)//热力图接口
            angleMeterChartForOne(jizu, start, end)//偏差角度仪表图3~7.5m/s偏差
            angleMeterChartForTwo(jizu, start, end)//偏差角度仪表图7.5~11.2m/s偏差
            angleListTable(jizu, start, end)//偏差角度列表数据
            angleList(areaValue, stationValue, jizu, start, end)//偏航偏差角度折线图
            // }
        }

        function parseTime(time) {
            var mydate = new Date(parseInt(time));
            var year = mydate.getFullYear();
            var month = mydate.getMonth() + 1;
            var day = mydate.getDate();

            return year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
        }



        //上传文件
        function getFileName(path) {//获取文件名
            var pos1 = path.lastIndexOf('/');
            var pos2 = path.lastIndexOf('\\');
            var pos = Math.max(pos1, pos2)
            if (pos < 0)
                return path;
            else
                return path.substring(pos + 1);
        }
        //上传文件onchange触发的事件
        function showsize(files) {

            var filename = getFileName(files.value);
            var ext = filename.substr(filename.lastIndexOf(".")).toLowerCase()//获取文件名后缀
            if (ext != ".xls" && ext != ".xlsx") {
                alert("文件格式错误，要求是excel文件");
                return false;
            }
            checkFileSize(files);//校验文件大小
        }
        //校验文件大小
        function checkFileSize(files) {
            var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
            var fileSize = 0;
            if (isIE && !files) {
                var filePath = files.value;
                var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                var file = fileSystem.GetFile(filePath);
                fileSize = files.size;
            } else {
                fileSize = files.size;
            }
            var fsize = fileSize / (1024 * 1024);
            if (fsize < 60) {//上传的文件
                upload();//调用ajax的上传事件
            } else {
                alert("文件太大，上传的文件超过60M,请重新上传");
                return false;
            }
        }


        //接口返回 赋值函数
        //热力图
        function setHeartChart(data) {
            if (data != "" && data != undefined) {
                if (data[0].x != undefined && data[0].y != undefined && data[0].point != undefined) {
                    var length = data[0].y.length;
                    var min = 0;
                    var max = 0;
                    for (var i = 0; i < data[0].point.length; i++) {
                        if (max < data[0].point[i][2]) {
                            max = data[0].point[i][2];
                        }
                    }

                    dataModel.getDataByTag("heatchart").a("xAxis", data[0].x);
                    dataModel.getDataByTag("heatchart").a("yAxis", data[0].y);
                    dataModel.getDataByTag("heatchart").a("getData", data[0].point);
                    dataModel.getDataByTag("heatchart").a("min", 0);
                    dataModel.getDataByTag("heatchart").a("max", parseInt(max) + 1);
                }
            }
        }
        //偏差角度仪表图3~7.5m/s偏差
        function setAngleChartOne(data) {

            if (data != "" && data != undefined) {
                if (data[0].value != undefined) {
                    var max = parseInt(data[0].value / 5) * 6

                    dataModel.getDataByTag("positionDeviation1").a("max", max);
                    dataModel.getDataByTag("positionDeviation1").a("min", -max);
                    dataModel.getDataByTag("positionDeviation1").a("getData", data[0].value);
                }
            }
        }
        //偏差角度仪表图7.5~11.2m/s偏差
        function setAngleChartTwo(data) {
            if (data != "" && data != undefined) {
                if (data[1] != undefined && data[1].value != undefined) {
                    var max = parseInt(data[1].value / 5) * 6

                    dataModel.getDataByTag("positionDeviation2").a("max", max);
                    dataModel.getDataByTag("positionDeviation2").a("min", -max);
                    dataModel.getDataByTag("positionDeviation2").a("getData", data[1].value);
                } else {
                    var max = parseInt(data[0].value / 5) * 6

                    dataModel.getDataByTag("positionDeviation2").a("max", max);
                    dataModel.getDataByTag("positionDeviation2").a("min", -max);
                    dataModel.getDataByTag("positionDeviation2").a("getData", data[0].value);
                }
            }
        }
        function setAngleListTable(data) {
            dataModel.getDataByTag("table").a("ht.dataSource", data);
        }
        //偏航偏差角度折线图
        function setAngleList(data) {
            dataModel.getDataByTag("angleLine").a("xAxis", data.xData);
            dataModel.getDataByTag("angleLine").a("yAxis", data.yData);
        }

        function setCalculate(data) {
            console.log("data:", data)
            var yaw = data.yaw;//热力图数据
            var windAngle = data.windAngle;//表格数据
            var deviation = data.deviation;//仪表盘数据
            var chart = data.chart;//折线图数据
            setHeartChart(yaw);
            setAngleChartOne(deviation);
            setAngleChartTwo(deviation);
            setAngleListTable(windAngle);
            setAngleList(chart);
        }

        /**
         * 监听页面的点击事件
         * */
        graphView.addInteractorListener(function (e) {
            var data = e.data;
            var kind = e.kind;
            var type = e.type;

            if (kind == 'onClick' && type == "comp") {
                if (data._tag == "introduction") {//点击导入 触发的事件
                    document.getElementById("btn_file").click();

                } if (data._tag == 'calc') {//点击计算
                    calculate("yaw-deviation")//计算接口
                }
                if (data._tag == "search") {//点击搜索 触发的事件
                    var areaData = data._dataModel.getDataByTag("area").a("ht.value");
                    var stationData = data._dataModel.getDataByTag("station").a("ht.value")
                    var setData = data._dataModel.getDataByTag("set").a("ht.value")
                    var startTimeData = data._dataModel.getDataByTag("startTime").a("ht.value")
                    var endTimeData = data._dataModel.getDataByTag("endTime").a("ht.value")
                    var start = new Date(startTimeData + " 00:00:00")
                    var end = new Date(endTimeData + " 00:00:00")
                    angleThermodynamicChart(setData, start.valueOf(), end.valueOf())//热力图接口
                    angleMeterChartForOne(setData, start.valueOf(), end.valueOf())//偏差角度仪表图3~7.5m/s偏差
                    angleMeterChartForTwo(setData, start.valueOf(), end.valueOf())//偏差角度仪表图7.5~11.2m/s偏差
                    angleListTable(setData, start.valueOf(), end.valueOf())//偏差角度列表数据
                    angleList(areaData, stationData, setData, start.valueOf(), end.valueOf())//偏差角度折线图
                }
                if (data._tag == "export") {

                    var table = document.getElementById("tableExcel");
                    var column = dataModel.getDataByTag("table").a("ht.columns");
                    var dataSource = dataModel.getDataByTag("table").a("ht.dataSource");

                    var tableHeader = "<tr>";
                    for (var i = 0; i < column.length; i++) {
                        tableHeader += "<td>" + column[i].displayName + "</td>"
                    }

                    tableHeader += '</tr>';

                    var tableBody = "";
                    for (var i = 0; i < dataSource.length; i++) {
                        tableBody += "<tr>"
                        tableBody += `<td style='mso-number-format:"\@"'><label>` + dataSource[i].windRange + `</label></td>`
                        tableBody += "<td>" + dataSource[i].angle + "</td>"
                        tableBody += "<td>"
                    }
                    table.innerHTML = tableHeader;
                    table.innerHTML += tableBody;

                    $("#tableExcel").table2excel({
                        //exclude: ".noExl",
                        name: "sheet1",
                        filename: "机组偏航偏差-" + new Date().toISOString().replace(/[\-\:\.]/g, "") + ".xls",
                        fileext: ".xls",
                    });
                }
            }

        });
        //监听页面下拉框的选择事件
        dataModel.addDataPropertyChangeListener(e => {
            var property = e.property;
            var data = e.data;
            var viewUrl;

            if (data._tag == 'area') {//点击区域
                if (property == 'a:valueId') {
                    var id = data.a("valueId")
                    var stationArr = getStation(id);//请求场站接口
                    if (stationArr != "") {
                        data._dataModel.getDataByTag("station").a("ht.dataSource", stationArr)
                    }
                }
            }
            if (data._tag == 'station') {//点击场站
                if (property == 'a:valueId') {
                    var fanArr = getFan(id);//请求机组接口
                    if (fanArr != "") {
                        data._dataModel.getDataByTag("set").a("ht.dataSource", fanArr)
                    }
                }
            }

        })

    </script>
</head>

<body onload='init()'>
    <input type="file" name="file1" onchange="showsize(this)" id="btn_file" style="display: none;">
    <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0" style="display: none;">

    </table>
</body>

</html>
